package com.iceland.common.rpc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RpcServiceClassFactory {

    public RpcServiceClassFactory(){

    };
    private static Map<String,Class> classMap=new ConcurrentHashMap<>();
    private static Map<String,Object> beanMap=new ConcurrentHashMap<>();
    public static void putBean(Class _class){
        RemoteService remoteService = (RemoteService)_class.getAnnotation(RemoteService.class);
        classMap.put(remoteService.path(),_class);
    }
    public static Object getBean(String path){
        Object bean=beanMap.get(path);
        if(bean==null){
            Class aClass = classMap.get(path);
            if(aClass==null){
                return null;
            }
            try {
                bean = aClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
                return null;
            }
            beanMap.put(path,bean);

        }
        return bean;
    }
}
